package com.visionblue.business.ship.page.collection.panel

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import com.visionblue.business.ship.R
import com.visionblue.business.ship.base.BaseFragment
import com.visionblue.business.ship.page.collection.CollectionActivity
import com.visionblue.business.ship.page.collection.CollectionViewModel
import com.visionblue.business.ship.page.collection.panel.flow.FlowChildFragment
import com.visionblue.business.ship.page.collection.panel.weight.WeightFragment
import com.visionblue.business.ship.page.outer.OuterActivity
import com.visionblue.business.ship.page.register.config.*
import com.visionblue.business.ship.util.ext.setVisible
import kotlinx.android.synthetic.main.frag_panel.*
import org.koin.androidx.viewmodel.ext.android.sharedViewModel

/**
 * @author: xlh
 * @date: 2020-05-20 4:00 PM
 * @desc:
 */
class PanelFragment : BaseFragment() {

    private val mViewModel: CollectionViewModel by sharedViewModel()

    override val layoutId: Int = R.layout.frag_panel

    override fun initView(savedInstanceState: Bundle?) {

        val flowFragment = genFlowFragment()
        val weightFragment = genWeightFragment()
        childFragmentManager
            .beginTransaction()
            .add(R.id.flPanelContent, flowFragment)
            .add(R.id.flPanelContent, weightFragment)
            .commit()

        rgCalculate.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.rbFlow -> {
//                    mViewModel.currWorkTypeLiveData.value = WorkType.FLOW
//                    genFlowFragment()
                    childFragmentManager.beginTransaction()
                        .hide(weightFragment)
                        .show(flowFragment)
                        .commit()
                }
                R.id.rbWeigh -> {
//                    mViewModel.currWorkTypeLiveData.value = WorkType.WEIGHT
//                    genWeightFragment()
                    childFragmentManager.beginTransaction()
                        .hide(flowFragment)
                        .show(weightFragment)
                        .commit()
                }
//                else -> null
            }

//            fragment?.let {
//                childFragmentManager.beginTransaction().replace(R.id.flPanelContent, fragment)
//                    .commit()
//            }
        }


//        rgCalculate.check(R.id.rbFlow)
    }

    override fun initData() {
        super.initData()

        mViewModel.menuItemsLiveData.observe(this, Observer { menuItems ->

            rbFlow.setVisible(false)
            rbWeigh.setVisible(false)

            val flowResult =
                menuItems.find { it.isFlow()}
            val weightResult = menuItems.find { it.isSolid() }
            if (flowResult != null && weightResult != null) {
                rbFlow.setVisible(true)
                rbWeigh.setVisible(true)//暂时隐掉重量
                rgCalculate.check(R.id.rbFlow)
            } else {
                if (flowResult != null) {
                    rgCalculate.check(R.id.rbFlow)
                }

                if (weightResult != null) {
                    rgCalculate.check(R.id.rbWeigh)
                }
            }
        })

        val activity = activity
        if (activity is CollectionActivity) {
            activity.isOutput()
        } else {
            null
        }?.let {
            mViewModel.loadMenuItems(it)
        }

    }


//    private fun genFragment(it: WorkStatus?): BaseFragment? {
//        return mViewModel.checkCurrWorkStatus(
//            it, beforeWork = if (rgCalculate.checkedRadioButtonId == R.id.rbFlow) {
//                BeforeFlowWorkFragment.newInstance()
//            } else {
//                BeforeWeightWorkFragment.newInstance()
//            }, working = if (rgCalculate.checkedRadioButtonId == R.id.rbFlow) {
//                FlowWorkingFragment.newInstance()
//            } else {
//                WeightWorkingFragment.newInstance()
//            }
//        )
//    }

    private fun genFlowFragment(): FlowChildFragment {
//        return mViewModel.checkCurrWorkStatus(
//            beforeWork = BeforeFlowWorkFragment.newInstance(),
//            working = FlowWorkingFragment.newInstance()
//        )
        return FlowChildFragment.newInstance()
    }

    private fun genWeightFragment(): WeightFragment {
//        return mViewModel.checkCurrWorkStatus(
////            beforeWork = BeforeWeightWorkFragment.newInstance(),
////            working = WeightWorkingFragment.newInstance()
////        )
        return WeightFragment.newInstance()
    }

    companion object {
        fun newInstance(): PanelFragment {
            return PanelFragment()
        }
    }
}